package controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import domain.Clothes;
import domain.Clothesout;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.ClothesService;
import utils.DateGridViewResult;
import vo.Clothesvo;
import vo.Orderdetvo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : ClothesController
 * @Description : //服装控制器类
 * @Author : LYH
 * @Date: 2022/5/27  15:50
 */
@Controller
public class ClothesController {
    //服装业务服务类
    @Autowired
    private ClothesService clothesService;
//服装查询方法
@RequestMapping("/clothes_search")
@ResponseBody
    public DateGridViewResult clothes_search(Clothesvo clothesvo){
    //调用mybatis的分页插件
    PageHelper.startPage(clothesvo.getPage(),clothesvo.getLimit());
    //接受查找到的服装集合
    List<Clothes> clothesList= clothesService.findClothes_List(clothesvo);
    //完成分页
    PageInfo<Clothes> pageInfo = new PageInfo<>(clothesList);
    //向前端发送数据
    return new DateGridViewResult(pageInfo.getTotal(),pageInfo.getList());
}
//服装添加方法
@RequestMapping("/clothes_add")
@ResponseBody
    public Map<String,Object> clothesAdd(Clothes clothes){
    //创建map集合，用于储存操作结果
    Map<String,Object> map=new HashMap<>();
    //验证数据库，判断数据库中是否已有相同的商品
    Clothes clothes1=clothesService.findclothes(clothes);
    //若不为空，则数据库中已有该商品，向前端反馈操作失败
    if(clothes1!=null){
        map.put("result",false);
        return map;
    }
    //接受数据库中影响的行数
    int flag=clothesService.insertclothes(clothes);
    //若大于0，说明操作成功
    if(flag>0){
        map.put("result",true);
    }else {
        map.put("result",false);
    }

    return map;
}
//服装更新方法
@RequestMapping("/clothes_update")
@ResponseBody
    public Map<String,Object> clothesUpdate(Clothes clothes){
        Map<String,Object> map=new HashMap<>();
        //判断数据库 是否已存在相同信息的商品
        Clothes clothes1=clothesService.findclothes(clothes);
        //不为空，说明存在，返回结果为失败
        if(clothes1!=null){
            map.put("result",false);
            return map;
        }
        //为空，调用更新服装信息的服务
        int flag=clothesService.updateclothes(clothes);
        //影响行数大于0，说明数据库操作成功
        if(flag>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }
        return map;
    }
//服装删除方法
@RequestMapping("/clothes_delete")
@ResponseBody
    public Map<String,Object> clothesDelete(Clothes clothes){
        System.out.println(clothes.toString());
        Map<String,Object> map=new HashMap<>();
        //接收数据库影响的行数
        int flag=clothesService.deleteclothes(clothes);
        if(flag>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }
    return map;
    }
    //服装批量删除方法
    @RequestMapping("/clothes_deleteList")
    @ResponseBody
    public Map<String,Object> clothesDeleteList(@RequestBody List<Clothes> clothesList){
        Map<String,Object> map=new HashMap<>();
        int flag=0;
        //遍历接收的list集合，循环调用删除单个的方法
        for (Clothes clothes : clothesList) {
            System.out.println(clothes.toString());
            flag=clothesService.deleteclothes(clothes);
        }
        if(flag>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }

        return map;
    }


}
